package cl.tinet.trainee.proyecto.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * Encriptacion en MD5.
 * 
 * @author {@link http://www.asjava.com/core-java/java-md5-example/}
 */
public class MD5 {
    /**
     * Largo para char 32.
     */
    private static final int LARGO = 32;
    /**
     * Largo para char 16.
     */
    private static final int CORTO = 16;

    /**
     * 
     * Constructor MD5.
     * 
     */
    public MD5() {
        super();
    }

    /**
     * 
     * getMD5 encripta un String en formato md5.
     * 
     * @param input
     *            String a encriptar.
     * @return hashtext String encriptado.
     */
    public static String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(CORTO);
            // Now we need to zero pad it if you actually want the full 32
            // chars.
            while (hashtext.length() < LARGO) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}
